﻿using LaoLuo.Time;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace LaoLuo.Common.Test
{


    /// <summary>
    ///这是 WeeklyTimetableTest 的测试类，旨在
    ///包含所有 WeeklyTimetableTest 单元测试
    ///</summary>
    [TestClass()]
    public class WeeklyTimetableTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///WeeklyTimetable 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void WeeklyTimetableConstructorTest()
        {
            var p1 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Saturday, 9, 0),
new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 0, 55));
            var p2 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 9, 0), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 0, 55));

            var table = new WeeklyTimetable(p1, p2);

            Assert.AreEqual(p2, table.GetParentPeriodOrDefault(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 11)));
            Assert.IsNull(table.GetParentPeriodOrDefault(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 11)));

            bool b;
            var properDateTime = table.GetProperPeriod(new DateTime(2012, 1, 18, 0, 0, 0), out b);

            Assert.IsFalse(b);
            Assert.AreEqual(new DateTime(2012, 1, 15, 9, 0, 0), properDateTime[0]);
            Assert.AreEqual(new DateTime(2012, 1, 16, 0, 55, 0), properDateTime[1]);

            var properPoint = table.GetProperPeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Saturday, 11), out b);

            Assert.IsTrue(b);
            Assert.AreEqual(p1, properPoint);

            var p3 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Friday, 10), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 10));
            table = new WeeklyTimetable(p3);

            properPoint = table.GetProperPeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Saturday, 10), out b);
            Assert.IsTrue(b);
            Assert.AreEqual(p3, properPoint);

            properPoint = table.GetProperPeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 10), out b);
            Assert.IsTrue(b);
            Assert.AreEqual(p3, properPoint);

            var p4 = p3.GetReversed();
            table = new WeeklyTimetable(p4);

            properPoint = table.GetProperPeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Saturday, 10), out b);
            Assert.IsFalse(b);
            Assert.AreEqual(p4, properPoint);

            properPoint = table.GetProperPeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 10), out b);
            Assert.IsFalse(b);
            Assert.AreEqual(p4, properPoint);
        }

        [TestMethod()]
        public void WeeklyTimePointTest()
        {
            Assert.AreEqual(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 10), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 10).Add(24));
            Assert.AreEqual(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Sunday, 10), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Saturday, 10).Add(24));

            var closed1 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 0, 0, 0, true), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true));
            var closed2 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 0, 0, 0, true), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true));
            var closed3 = new WeeklyTimePeriod(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 0, 0, 0, true), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 1, true));

            Assert.AreEqual(closed1, closed2);
            Assert.IsTrue(closed1 == closed2);
            Assert.IsFalse(closed1 == closed3);

            Assert.IsTrue(closed1.EndPointIsAfter(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true)));
            Assert.IsTrue(closed1.StartPointIsBefore(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Monday, 0, 0, 0, true)));
            Assert.IsTrue(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true).Equals(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true)));
            Assert.IsFalse(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true).Equals(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 1, true)));
            Assert.AreEqual(new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true).GetHashCode(), new WeeklyTimePeriod.WeeklyTimePoint(DayOfWeek.Tuesday, 0, 0, 0, true).GetHashCode());
        }
    }
}
